package example.dao;

import com.alicloud.openservices.tablestore.model.*;
import example.config.db.TableStoreBaseDB;
import example.core.log.FcLog;
import example.entity.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TestDao extends TableStoreBaseDB {

    @Autowired
    FcLog fcLog;

    /**
     * 测试读取一行数据
     * @return
     */
    public Test getRow() {
        //构造主键。
        PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
        primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromLong(123));
        PrimaryKey primaryKey = primaryKeyBuilder.build();

        //读取一行数据，设置数据表名称。
        SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("test", primaryKey);

        //设置读取最新版本。
        criteria.setMaxVersions(10);
        GetRowResponse getRowResponse = getSyncClient().getRow(new GetRowRequest(criteria));
        Row row = getRowResponse.getRow();
        Test test = new Test();
        test.setName(row.getPrimaryKey().getPrimaryKeyColumn("name").getValue().toString());
        test.setId(Integer.valueOf(row.getPrimaryKey().getPrimaryKeyColumn("id").getValue().toString()));
        return test;
    }
}
